#include<stdio.h>
#include<stdlib.h>
#include "fifo.h"

fifo fifo_create (long size) {
  fifo myfifo;
  
  myfifo.t = malloc(size * sizeof(myfifo.t));
  myfifo.first = &myfifo.t[0]; // fifo head points to the first element 
  myfifo.last = myfifo.first; // the fifo is empty

  return myfifo;
}

void fifo_add (fifo *f, long a) {
  *(f->last) = a; // new element will be added to the last position
  f->last++ ; 
}

long fifo_extract(fifo *f) {
  long extract; // the member that will be extracted

  extract = *(f->first); // extract the first element of the fifo
  f->first++ ;
  
  return extract;
}

int fifo_empty(fifo *f) {
  if(f->first == f->last){return 0;}
  else {return 1;}
}

void fifo_clear(fifo *f){
  f->first = &(f->t[0]); 
  f->last = f->first;
}
